package cn.news.xinhuanet.utils;

import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * Created by xinhuanet on 16-2-4.
 */
public class ToolUtils {
    public static boolean isAjax(HttpServletRequest request) {
        System.out.println("request:" + request.getHeader("accept"));
        return request
                .getHeader("accept")
                .indexOf("application/json") > -1 ||
                (request.getHeader("X-Requested-With") != null
                        && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1);
    }

    public static Integer getRandomInt(int size) {
        if (size <= 0) return 0;
        return new Random().nextInt(size);
    }

    @Test
    public void test() {
        while (true) {
            System.out.println(getRandomInt(10));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
